﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using PhoneAppSTS.Utils;
using STS.Model;

namespace PhoneAppSTS
{
    public partial class StateMedication : PhoneApplicationPage
    {
        private VisitUtils visitUtils;
        public StateMedication()
        {
            InitializeComponent();
            visitUtils = new VisitUtils();
            this.DataContext = visitUtils;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            InitListaWizyt();

            ModelInstance.WizytaViewModel.FindWizytaItems();
            if (ModelInstance.WizytaViewModel.WizytaItems.Count == 0)
            {
                visitUtils.DownloadWizyty(102);
            }
            else
            {
                ModelInstance.WizytaViewModel.FindWizytaItems();
                visitUtils.DatyWizyt.Clear();
                foreach (var item in ModelInstance.WizytaViewModel.WizytaItems)
                {
                    DataWizyty dw = new DataWizyty((DateTime)item.Data, item.IdWizyty);
                    visitUtils.DatyWizyt.Add(dw);
                }

                visitUtils.Wizyta = ModelInstance.WizytaViewModel.WizytaItems[0];
            }
        }

        private void InitListaWizyt()
        {
            ListBoxWizyty.ItemsSource = visitUtils.DatyWizyt;
        }

        private void ListBoxWizyty_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataWizyty dt = (sender as DataWizyty);
            if (dt != null)
            {
                Wizyta wiz = new Wizyta();
                foreach (var item in ModelInstance.WizytaViewModel.WizytaItems)
                {
                    if (item.IdWizyty == dt.IdWizyty)
                    {
                        wiz.Cisnienie = item.Cisnienie;
                        wiz.Waga = item.Waga;

                        break;
                    }
                }

                visitUtils.Wizyta = wiz;
            }

        }
    }
}